﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Web.Security;

namespace Framework.WebmasterTool
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadLink();
                LoadModule();
            }
        }

        public void LoadLink()
        {
            aCompany.HRef = Globals.ApplicationPath + "WebmasterTool/Default.aspx";
            aCategory.HRef = Globals.ApplicationPath + "WebmasterTool/Categories.aspx";
            aArticle.HRef = Globals.ApplicationPath + "WebmasterTool/Articles/Default.aspx";
            aProduct.HRef = Globals.ApplicationPath + "WebmasterTool/Products/Default.aspx";
            aContact.HRef = Globals.ApplicationPath + "WebmasterTool/Contacts/Default.aspx";
            aWatermark.HRef = Globals.ApplicationPath + "WebmasterTool/Watermark.aspx";
            aManagerUser.HRef = Globals.ApplicationPath + "WebmasterTool/Memberships/ManageUsers.aspx";
            aSlideShow.HRef = Globals.ApplicationPath + "WebmasterTool/SlideShow.aspx";
            aDownloadFile.HRef = Globals.ApplicationPath + "WebmasterTool/FileManagers/Default.aspx";
            aSupportOnline.HRef = Globals.ApplicationPath + "WebmasterTool/Supporter.aspx";
            aWeblink.HRef = Globals.ApplicationPath + "WebmasterTool/WebLink.aspx";
            aGalleryVideo.HRef = Globals.ApplicationPath + "WebmasterTool/VideoClips.aspx";
            aGalleryPhoto.HRef = Globals.ApplicationPath + "WebmasterTool/Photos/Default.aspx";
            aGalleryImage.HRef = Globals.ApplicationPath + "WebmasterTool/GalleryImages/Categories.aspx";
            aVote.HRef = Globals.ApplicationPath + "WebmasterTool/Votes/Default.aspx";
            aNewsLetter.HRef = Globals.ApplicationPath + "WebmasterTool/NewsLetter.aspx";
            aComment.HRef = Globals.ApplicationPath + "WebmasterTool/Comments/Articles.aspx";
            aAdvertising.HRef = Globals.ApplicationPath + "WebmasterTool/Advertisings/Default.aspx";
            aRewriteURL.HRef = Globals.ApplicationPath + "WebmasterTool/RewriteURL.aspx";
            aMailServer.HRef = Globals.ApplicationPath + "WebmasterTool/HostMail.aspx";
            aLogSystem.HRef = Globals.ApplicationPath + "WebmasterTool/Logs/Default.aspx";
            aReport.HRef = Globals.ApplicationPath + "WebmasterTool/Reports.aspx";
            aTag.HRef = Globals.ApplicationPath + "WebmasterTool/Tags/Default.aspx";
        }

        public void LoadModule()
        {
            MembershipUser someUser = Membership.GetUser();
            if (someUser == null)
            {
                Response.Redirect(Globals.ApplicationPath + "LoginAdmin/Default.aspx");
            }
            else
            {
                lblUsername.InnerText = someUser.UserName;
                string strAdministrator = "Super Administrator";
                if (someUser.UserName == "administrator" || Roles.IsUserInRole(someUser.UserName, strAdministrator) == true)
                {
                    aChangeAllPass.HRef = Globals.ApplicationPath + "WebmasterTool/Memberships/ResetPassword.aspx";
                    aChangeAllPass.Visible = true;
                }
                var user = entity.aspnet_Users.Where(us => us.UserName == someUser.UserName).FirstOrDefault();
                user.aspnet_Roles.Load();
                var rolesForUser = user.aspnet_Roles.ToList();
                foreach (var item in rolesForUser)
                {
                    var permission = entity.udv_Permissions.Where(ps => ps.RoleId == item.RoleId).FirstOrDefault();
                    if (permission.Advertising.Value == true)
                    {
                        aAdvertising.Visible = true;
                    }
                    if (permission.Article.Value == true)
                    {
                        aArticle.Visible = true;
                    }
                    if (permission.Category.Value == true)
                    {
                        aCategory.Visible = true;
                    }
                    if (permission.Comment.Value == true)
                    {
                        aComment.Visible = true;
                    }
                    if (permission.Company.Value == true)
                    {
                        aCompany.Visible = true;
                    }
                    if (permission.Contact.Value == true)
                    {
                        aContact.Visible = true;
                    }
                    if (permission.DownloadFile.Value == true)
                    {
                        aDownloadFile.Visible = true;
                    }
                    if (permission.GalleryImage.Value == true)
                    {
                        aGalleryImage.Visible = true;
                    }
                    if (permission.GalleryVideo.Value == true)
                    {
                        aGalleryVideo.Visible = true;
                    }
                    if (permission.GalleryPhoto.Value == true)
                    {
                        aGalleryPhoto.Visible = true;
                    }
                    if (permission.Mailserver.Value == true)
                    {
                        aMailServer.Visible = true;
                    }
                    if (permission.ManagerUser.Value == true)
                    {
                        aManagerUser.Visible = true;
                    }
                    if (permission.LogSystem.Value == true)
                    {
                        aLogSystem.Visible = true;
                    }
                    if (permission.Report.Value == true)
                    {
                        aReport.Visible = true;
                    }
                    if (permission.Tag.Value == true)
                    {
                        aTag.Visible = true;
                    }
                    if (permission.NewsLetter.Value == true)
                    {
                        aNewsLetter.Visible = true;
                    }
                    if (permission.Product.Value == true)
                    {
                        aProduct.Visible = true;
                    }
                    if (permission.RewriteURL.Value == true)
                    {
                        aRewriteURL.Visible = true;
                    }
                    if (permission.SlideShow.Value == true)
                    {
                        aSlideShow.Visible = true;
                    }
                    if (permission.SupportOnline.Value == true)
                    {
                        aSupportOnline.Visible = true;
                    }
                    if (permission.Vote.Value == true)
                    {
                        aVote.Visible = true;
                    }
                    if (permission.Watermark.Value == true)
                    {
                        aWatermark.Visible = true;
                    }
                    if (permission.Weblink.Value == true)
                    {
                        aWeblink.Visible = true;
                    }
                }
            }
        }
    }
}